<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select
        v-model="listQuery.sid"
        class="filter-item"
        style="width: 150px;"
        filterable
        clearable
        :loading="loading"
        placeholder="选择区服"
      >
        <el-option v-for="item in serverList" :key="item.sid" :label="item.name + '(' + item.sid + ')'" :value="item.sid">
        </el-option>
      </el-select>
      <el-date-picker
        v-model="listQuery.logDt[0]"
        class="filter-item"
        type="date"
        style="width: 140px;"
        placeholder="记录时间"
      ></el-date-picker>-
      <el-date-picker
        v-model="listQuery.logDt[1]"
        class="filter-item"
        type="date"
        style="width: 140px;"
        placeholder="记录时间"
      ></el-date-picker>
      <el-input v-model="listQuery.actor" class="filter-item" style="width: 250px;" placeholder="账号/角色ID/角色名" />
      <el-button class="filter-item" type="primary" plain icon="el-icon-search" @click="handleFilter">搜索</el-button>
      <el-button
        :loading="downloadLoading"
        type="primary"
        icon="el-icon-download"
        class="filter-item"
        @click="handleDownload()"
      >导出</el-button>
    </div>
    <el-table ref="table" v-loading="listLoading" :data="list" stripe border class="table-content" height="675">
      <el-table-column
        v-for="(column, index) in allColumns"
        :key="index"
        :prop="column.name"
        :label="column.label"
        :width="column.width || '70'"
        :align="column.align || 'center'"
        :fixed="column.fixed || false"
        :sortable="column.sortable || false"
      >
        <template slot-scope="{ row }">
          <span v-if="column.formatter">{{ column.formatter(row) }}</span>
          <span v-else>{{ row[column.name] }}</span>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
  </div>
</template>

<script>

import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'
import { getDauActorStaticList } from '@/api/actorInfo'
import { getServerList } from '@/api/ops'
import { deepClone } from '@/utils/index'
export default {
  name: 'DailyStatics',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      tableKey: 0,
      list: [],
      serverList: [],
      total: 0,
      loading: false,
      listLoading: false,
      downloadLoading: false,
      listQuery: {
        page: 1,
        limit: 200,
        logDt: [new Date(Date.now() - 86400000 * 9), new Date()],
        sid: '',
        actor: ''
      },
      columns: [],
      baseData: [
        { name: 'log_date', label: '记录时间', width: '100', fixed: true, align: 'center' },
        { name: 'sid', label: '服务器', width: '70', fixed: true, align: 'center' },
        { name: 'account', label: '账号', width: '160', fixed: true, align: 'center' },
        { name: 'actor_id', label: '角色ID', width: '100', fixed: true, align: 'center' },
        { name: 'actor_name', label: '角色名', width: '90', fixed: true, align: 'center' }
      ],
      exportList: []
    }
  },
  computed: {
    allColumns() {
      let allColumns = [
        ...this.baseData
      ]
      allColumns = allColumns.concat(deepClone(this.columns))
      return allColumns
    },
    tHeader() {
      return this.allColumns.map(column => column.label)
    },
    filterVal() {
      return this.allColumns.map(column => column.name)
    }
  },
  mounted() {

  },
  created() {
    this.getServerList()
    // this.getList()
  },
  methods: {
    async getList() {
      const query = this.listQuery
      this.listLoading = true
      const res = await getDauActorStaticList(query)
      this.listLoading = false
      this.list = res.data.items
      this.total = res.data.total
      const columns = []
      for (const key in res.data.labelList) {
        columns.push({
          name: key,
          label: res.data.labelList[key],
          width: '100',
          align: 'center'
        })
      }
      this.columns = columns
      this.$nextTick(() => {
        this.$refs.table.doLayout()
      })
      console.log(this.allColumns)
    },
    handleFilter(query) {
      this.searchBarQuery = query
      this.getList()
    },
    async getServerList(params) {
      const query = params
      const res = await getServerList(query)
      this.serverList = res.data.items
      this.loading = false
    },
    handleDownload() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = this.tHeader
        const filterVal = this.filterVal
        getDauActorStaticList(Object.assign({}, this.listQuery, { export: 1 })).then(response => {
          this.dlist = response.data.items
          const data = this.dlist.map(v => filterVal.map(j => {
            return v[j]
          }))
          excel.export_json_to_excel({
            header: tHeader,
            data,
            filename: '每日角色统计'
          })
          this.downloadLoading = false
        })
      })
    }
  }
}
</script>
